#include "main.h"
#include "scnsInclude.h"
#include "rtc.h"
#include "hxMqtt.h"

uint8 __readTest=0;

static void readTest(void)
{
    if(__readTest)
    {
        ScnsLcdLayer layN=(SCNS_LCD_LAYER_AUTO);
        static ScnsLcdColor tmp[60*60];
        {
            scnsLcdLayerAutoBegin();
            scnsLcdSetAddress(layN,(Point){0,0},(Point){0+60-1,0+60-1},0);
            scnsLcdReadColors(layN,tmp,60*60);
            scnsLcdLayerAutoEnd();
        }
        scnsLcdDisplayArrayImage(SCNS_LCD_LAYER_AUTO,(Point){0,scnsLcdGetBorder().y-60},tmp,SCNS_LCD_COLOR_FORMAT,NULL,60,60,0,0,0);
    }
}

void loop(void)
{
    scnsMenuFresh();
    scnsIwdgFeed();
    hxMqttPing();
    readTest();
    {
        static uint8 last=0;
        uint8 this=(scnsMenuCanPageDisplay(((uint8)-1))==1);
        if(this) scnsSmctpExtLoggerDisplay(SCNS_LCD_LAYER_AUTO,(Point){0,scnsLcdGetBorder().y-scnsLcdFontConsolas10.fontHeight*6},6,scnsLcdGetBorder().x,last!=this,6,&scnsLcdFontConsolas10,SCNS_LCD_COLOR_RED,SCNS_LCD_COLOR_BACKGROUND,0);
        last=this;
    }
    {
        RTC_TimeTypeDef rtcTime;
        RTC_DateTypeDef rtcDate;
        HAL_RTC_GetTime(&hrtc,&rtcTime,RTC_FORMAT_BIN);
        HAL_RTC_GetDate(&hrtc,&rtcDate,RTC_FORMAT_BIN);
        char str[128];
        hxSNPrintf(str,sizeof(str),"%04d-%02d-%02d %02d:%02d:%02d\n",2000+rtcDate.Year,rtcDate.Month,rtcDate.Date,rtcTime.Hours,rtcTime.Minutes,rtcTime.Seconds);
        scnsLcdDisplayString(SCNS_LCD_LAYER_AUTO,(Point){.x=0,.y=300},str,&scnsLcdFontConsolas16,SCNS_LCD_COLOR_RED,SCNS_LCD_COLOR_BACKGROUND,0);
    }
}
